// pages/login/register.js
const {
  weixin
} = require('../../utils/value.js');
const app = require('../../utils/ajax.js');
const md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    telephone: '',
    password: '',
    confirmPassword: '',
    Verification_data: '',
    getCodeText: '获取验证码',
    minutes: 60,
    interval: null,
    canGetCode: true,
    array: ['男', '女'],
    active: true,
    sexIndex: -1,
    name: ''
  },

  //跳转登录
  toLogin() {
    wx.redirectTo({
      url: '../login/login',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //解决下拉不回弹
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 处理手机号码变化事件
   */
  handleAccountChange: function(e) {
    this.setData({
      telephone: e.detail.value
    })
  },
  /**
   * 发送短信获取验证码
   */
  getCode: function() {
    const {
      telephone,
      canGetCode
    } = this.data;
    if (!canGetCode) {
      return;
    }
    const _this = this;
    const checkResult = app.checkPhone(telephone);
    if (!checkResult) {
      wx.showToast({
        title: '手机格式错误',
        duration: 1000,
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '发送中',
    });
    app.doPost(weixin.SENDMSG, {
      telephone: telephone,
      weixin_id: app.getData('open_id')
    }, function(data) {
      if (data.state === 1) {
        wx.showToast({
          title: data.msg,
          icon: 'success',
          duration: 1000
        })
        _this.setData({
          canGetCode: false
        });
        _this.data.interval = setInterval(function() {
          let minutes = _this.data.minutes;
          minutes -= 1;
          if (minutes <= 0) {
            _this.setData({
              minutes: 60,
              getCodeText: '重新发送',
              canGetCode: true
            });
            clearInterval(_this.data.interval);
          } else {
            _this.setData({
              minutes: minutes
            });
            _this.setData({
              getCodeText: minutes + 's后重新获取'
            });
          }
        }, 1000);
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  /**
   * 处理注册按钮事件
   */
  handleRegister: function(e) {
    const {
      telephone,
      Verification_data,
      name,
      ID_card,
      sexIndex,
      array,
      address
    } = this.data;
    const sex = array[sexIndex];
    if (!name) {
      wx.showToast({
        title: '姓名不能为空',
        duration: 1000,
        icon: 'none'
      })
      return;
    }
    if (!ID_card) {
      wx.showToast({
        title: '身份证号码不能为空',
        duration: 1000,
        icon: 'none'
      })
      return;
    }
    if (!telephone) {
      wx.showToast({
        title: '手机号不能为空',
        duration: 1000,
        icon: 'none'
      })
      return;
    }
    if (!Verification_data) {
      wx.showToast({
        title: '验证码不能为空',
        duration: 1000,
        icon: 'none'
      })
      return;
    }
    const submitData = {};
    submitData.telephone = telephone;
    submitData.weixin_id = app.getData('open_id');
    submitData.name = name;
    submitData.ID_card = ID_card;
    submitData.sex = sex;
    submitData.address = address;
    submitData.Verification_data = Verification_data;
    wx.showLoading({
      title: '注册中',
    })
    app.doPost(weixin.REGISTER, submitData, function(data) {
      if (data.state === 1) {
        wx.showToast({
          title: data.msg,
          icon: 'success',
          duration: 2000
        })
        //app.setToken(data.data.token);
        setTimeout(function(){
          wx.switchTab({
            url: '../index/index',
          })
        },1000)
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 姓名输入框
   */
  handleName: function(e) {
    this.setData({
      name: e.detail.value
    });
  },

  /**
   * 处理选择性别事件
   */
  bindPickerChange: function(e) {
    this.setData({
      sexIndex: e.detail.value
    })
  },

  /**
   * 处理身份证号码输入事件
   */
  handleStatus: function(e) {
    this.setData({
      ID_card: e.detail.value
    });
  },

  /**
   * 处理验证码输入事件
   */
  handleCode: function(e) {
    this.setData({
      Verification_data: e.detail.value
    });
  },

  /**
   * 处理地址输入事件
   */
  handleAddress: function(e) {
    this.setData({
      address: e.detail.value
    });
  }
})